﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace GPSTrackingWebsiteManager.Manager.UserControls
{
    public partial class ClientSearch : System.Web.UI.UserControl
    {
        //Events
        public delegate void AcceptEvent(Telerik.Web.UI.GridDataItem row);
        public event AcceptEvent OnAcceptEvent;
        public delegate void AddClientsEvent(Telerik.Web.UI.GridDataItem row);
        public event AddClientsEvent OnAddClientsEvent;

        //Propeties
        [Browsable(true), Category("Custom"), DefaultValue(true)]
        public bool IsAddClientsVisible { get; set; }

        public void ClearControls()
        {
            txtClientsID.Text = string.Empty;
            txtClientsName.Text = string.Empty;
            txtClientsLatitude.Text = string.Empty;
            txtClientsLongitude.Text = string.Empty;
            txtClientsPhone.Text = string.Empty;
            txtClientsEmail.Text = string.Empty;
        }

        public int GetClientID()
        {
            if (string.IsNullOrEmpty(txtClientsID.Text))
                return -1;
            else
                return Convert.ToInt32(txtClientsID.Text);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            btnAddClients.Visible = IsAddClientsVisible;
        }

        protected void btnClientsSearch_Click(object sender, EventArgs e)
        {
            GPSTrackingDBV2.Data.ClientsQuery ClientsQuery = new GPSTrackingDBV2.Data.ClientsQuery();
            if (!string.IsNullOrEmpty(txtSearchClientsID.Text)) ClientsQuery.AppendContains(GPSTrackingDBV2.Entities.ClientsColumn.ClientId, txtSearchClientsID.Text);
            if (!string.IsNullOrEmpty(txtSearchClientsName.Text)) ClientsQuery.AppendContains(GPSTrackingDBV2.Entities.ClientsColumn.Name, txtSearchClientsName.Text);
            if (!string.IsNullOrEmpty(txtSearchClientsLatitude.Text)) ClientsQuery.AppendContains(GPSTrackingDBV2.Entities.ClientsColumn.Latitude, txtSearchClientsLatitude.Text);
            if (!string.IsNullOrEmpty(txtSearchClientsLongitude.Text)) ClientsQuery.AppendContains(GPSTrackingDBV2.Entities.ClientsColumn.Longitude, txtSearchClientsLongitude.Text);
            if (!string.IsNullOrEmpty(txtSearchClientsPhone.Text)) ClientsQuery.AppendContains(GPSTrackingDBV2.Entities.ClientsColumn.Longitude, txtSearchClientsPhone.Text);
            if (!string.IsNullOrEmpty(txtSearchClientsEmail.Text)) ClientsQuery.AppendContains(GPSTrackingDBV2.Entities.ClientsColumn.Longitude, txtSearchClientsEmail.Text);

            ClientsGrid.DataSource = GPSTrackingDBV2.Data.DataRepository.ClientsProvider.Find(ClientsQuery);
            ClientsGrid.DataBind();
        }

        protected void btnClientsOK_Click(object sender, EventArgs e)
        {
            if (ClientsGrid.SelectedItems.Count > 0)
            {
                txtClientsID.Text = ClientsGrid.SelectedItems[0].Cells[3].Text;
                txtClientsName.Text = ClientsGrid.SelectedItems[0].Cells[4].Text;
                txtClientsLatitude.Text = ClientsGrid.SelectedItems[0].Cells[5].Text;
                txtClientsLongitude.Text = ClientsGrid.SelectedItems[0].Cells[6].Text;
                txtClientsPhone.Text = ClientsGrid.SelectedItems[0].Cells[7].Text;
                txtClientsEmail.Text = ClientsGrid.SelectedItems[0].Cells[8].Text;

                Telerik.Web.UI.GridDataItem row = ClientsGrid.SelectedItems[0] as Telerik.Web.UI.GridDataItem;

                if (OnAcceptEvent != null) OnAcceptEvent(row);
            }
        }

        protected void btnAddClients_Click(object sender, EventArgs e)
        {
            if (ClientsGrid.SelectedItems.Count > 0)
            {
                Telerik.Web.UI.GridDataItem row = ClientsGrid.SelectedItems[0] as Telerik.Web.UI.GridDataItem;
                OnAddClientsEvent(row);
            }
        }
    }
}